package com.deep.test.java8;

import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;
/**
 *  Predicate接口和lambda表达式  
    java.util.function.Predicate接口是用来支持java函数式编程新增的一个接口,
         使用这个接口和lamb表达式就可以以更少的代码为API方法添加更多的动态行为。   
 * @author hudepin
 *
 * 2018年2月9日上午10:44:16
 */
public class LambdaTest6 {
	public static void main(String[] args) {
		 List<String> languages = Arrays.asList("Java", "html5","JavaScript", "C++", "hibernate", "PHP");  
         
         //开头是J的语言  
         filter(languages,(name)->name.startsWith("J"));  
         //5结尾的  
         filter(languages,(name)->name.endsWith("5"));  
         //所有的语言  
         filter(languages,(name)->true);  
         //一个都不显示  
         filter(languages,(name)->false);  
         //显示名字长度大于4  
         filter(languages,(name)->name.length()>4);  
         System.out.println("-----------------------");  
         //名字以J开头并且长度大于4的  
         Predicate<String> c1 = (name)->name.startsWith("J");  
         Predicate<String> c2 = (name)->name.length()>4;  
         filter(languages,c1.and(c2));  
           
         //名字不是以J开头  
         Predicate<String> c3 = (name)->name.startsWith("J");  
         filter(languages,c3.negate());  
           
         //名字以J开头或者长度小于4的  
         Predicate<String> c4 = (name)->name.startsWith("J");  
         Predicate<String> c5 = (name)->name.length()<4;  
         filter(languages,c4.or(c5));  
           
         //名字为Java的  
         filter(languages,Predicate.isEqual("Java"));  
           
         //判断俩个字符串是否相等  
         boolean test = Predicate.isEqual("hello").test("world");  
         System.out.println(test);  
	}
	 public static void filter(List<String> languages, Predicate<String> condition) {    
         for(String name: languages) {    
             if(condition.test(name)) {    
                 System.out.println(name + " ");    
             }    
         }    
     }    
}
